package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.SysLogDao;
import com.itheima.domain.system.SysLog;
import com.itheima.factory.MapperFactory;
import com.itheima.service.system.SysLogService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class SysLogServiceImpl implements SysLogService {


    @Override
    public void save(SysLog sysLog) {

        SqlSession sqlSession = null;
        try {
            //获取sqlSession
            sqlSession = MapperFactory.getSqlSession();

            //获取Dao动态代理对象
            SysLogDao sysLogDao = MapperFactory.getMapper(sqlSession, SysLogDao.class);

            //id使用UUID随机生成
            String id = UUID.randomUUID().toString();
            sysLog.setId(id);

            //调用dao层代理对象的添加方法
            sysLogDao.save(sysLog);

            //提交事务
            TransactionUtil.commit(sqlSession);

        } catch (Exception e) {
            //回滚事务
            TransactionUtil.rollback(sqlSession);
            //抛出异常
            throw new RuntimeException(e);
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void delete(String id) {

        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            SysLogDao sysLogDao = MapperFactory.getMapper(sqlSession, SysLogDao.class);
            sysLogDao.delete(id);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public SysLog findById(String id) {

        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            SysLogDao sysLogDao = MapperFactory.getMapper(sqlSession, SysLogDao.class);
            return sysLogDao.findById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<SysLog> findAll() {

        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            SysLogDao sysLogDao = MapperFactory.getMapper(sqlSession, SysLogDao.class);
            return sysLogDao.findAll();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public PageInfo<SysLog> findAll(int page, int size) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            SysLogDao sysLogDao = MapperFactory.getMapper(sqlSession, SysLogDao.class);

            //使用分页插件进行分页查询
            PageHelper.startPage(page, size);

            List<SysLog> all = sysLogDao.findAll();
            return new PageInfo<>(all);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
